/**
 * Created by TomSean on 2017/5/25.
 */
var HabitManager = function ($parent) {
    this.$parent = $parent;
    this.sysUtils = new kendoUtils();
    this.init();
    return this;
}

HabitManager.prototype = {
    init : function () {
        var fieldArray = this.sysUtils.getBindFieldArray(this.$parent.find('.search-panel'));
        var functionArray = [
            {fName : 'search_data', fs : this.findData}
        ];
        this.model = new bindBaseModel(null, fieldArray, functionArray, null);
        this.sysUtils.bindDoucment(this.model, this.$parent, this);
        this.initTable();
        this.findData();
    },
    findData : function (e) {
        var $this = this;
        if (!isEmpty(e)) {
            $this = $(e.target).data('that');
        }
        var dataT = {'jsonParam' : JSON.stringify($this.sysUtils.getBindDocJson($this.model.bindFieldArray))};
        var dataSource = $this.sysUtils.getDataSource({url : baseApiPath + 'habit/list', data : dataT, dataType : 'odata'});
        $this.$parent.find('#habit_data_list').data('kendoGrid').setDataSource(dataSource);
    },
    initTable : function () {
        var $this = this;
        var columns = [
            {
                width : 30,
                headerTemplate : '<input type="checkbox" gridId="habit_data_list" onclick="gridCheckAll(this)" />',
                template : '<input type="checkbox" name="habit_data_id" value="${id}" />',
            },
            {
                title : '用户',
                template : '#:data.user.userName# (#:data.user.userAccount#)'
            },
            {
                title : '范围键',
                field : 'rangeKey'
            },
            {
                title : '设备键',
                field : 'deviceKey'
            },
            {
                title : '所存储的值',
                field : 'storageValue'
            },
            {
                title : '上一次使用时间',
                field : 'lastTime'
            }
        ];
        $this.sysUtils.createGird($this.$parent.find('#habit_data_list'), {
            selectable : 'single',
            autoSync : true,
            columnMenu : true,
            pageable : {input : true, numeric : false},
            columns : columns,
            change : function (e) {
                var selected = $.map(this.select(), function(itm) {
                    return $(itm).find('input[type=checkbox]').val();
                });
                $this.selectRowId = selected[0];
            }
        });
    }
};